import copy

import numpy as np

def Matrix(matrix):

    rows = len(matrix)
    cols = len(matrix[0])
    n = cols * cols
    m = rows*rows
    zero_matrix = np.zeros((n, m))

    for i in range(len(zero_matrix)):
        zero_matrix[i][i] = 1
        # 检查上方
        if i - cols >= 0:
            zero_matrix[i][i - cols] = 1
        # 检查下方
        if i + cols < n:
            zero_matrix[i][i + cols] = 1
        # 检查左侧
        if i % cols - 1 >= 0:
            zero_matrix[i][i - 1] = 1
        # 检查右侧
        if i % cols + 1 < cols:
            zero_matrix[i][i + 1] = 1
    # print(zero_matrix)
    zero_matrix=xor_inverse(zero_matrix)
    # print(f"关系矩阵",zero_matrix)

    for j in range(rows):
        for k in range(cols):
            matrix[j][k]= np.bitwise_xor.reduce(zero_matrix[j+k, :].astype(int))
    matrix = matrix.astype(int)

    print(matrix)
    k=find_ones_indices(matrix)
    print("下标",k)
    matrix_show(matrix)




    # return matrix
def xor_inverse(matrix):
    rank = np.linalg.matrix_rank(matrix)
    y = np.eye(len(matrix))
    print(f"当前矩阵主对角线情况",y)
    set_random_diagonal_elements(y,3,0)
    print(f"覆盖率为20%时主对角线情况",y)

    # for l in range(5):
    #     y[l][l]=0
    # print("满足60% ")
    # print(y)

    print("矩阵的秩为:", rank)
    n = len(matrix)
    augmented_matrix = np.hstack((matrix, y))
    # np.savetxt('augmented_matrix.txt', augmented_matrix)

    # print(augmented_matrix)

    for i in range(n):
        pivot_row = np.argmax(augmented_matrix[i:, i]) + i
        augmented_matrix[[i, pivot_row]] = augmented_matrix[[pivot_row, i]]

        diagonal_element = augmented_matrix[i, i]

        # Check if diagonal_element is zero before dividing
        if diagonal_element != 0:
            augmented_matrix[i, :] = augmented_matrix[i, :] / diagonal_element
        else:
            # Handle division by zero, for example, swap rows or skip division
            pass

        for j in range(n):
            if i != j and augmented_matrix[j, i] == 1:
                augmented_matrix[j, :] = np.logical_xor(augmented_matrix[j, :], augmented_matrix[i, :])

    inverse = augmented_matrix[:, n:]

    # Handle invalid values (NaN or inf) in the inverse matrix
    inverse[np.isnan(inverse)] = 0
    inverse[np.isinf(inverse)] = 0

    return inverse.astype(int)

def set_random_diagonal_elements(matrix, k, value):
    # 获取矩阵的行数和列数
    rows, cols = matrix.shape

    # 主对角线上的元素个数
    diagonal_size = min(rows, cols)

    # 确保 k 不超过主对角线上的元素个数
    k = min(k, diagonal_size)

    # 生成包含主对角线上所有元素下标的一维数组
    diagonal_indices = np.arange(diagonal_size)

    # 随机选择 k 个下标
    selected_indices = np.random.choice(diagonal_indices, k, replace=False)

    # 将选定的位置的元素设为指定的值
    matrix[selected_indices, selected_indices] = value
def matrix_show(matrix):
    # Calculate the number of rows and columns manually for a list of lists
    rows = len(matrix)
    cols = len(matrix[0]) if rows > 0 else 0

    deep_copy = copy.deepcopy(matrix)

    # Iterate through each element in the matrix
    for i in range(rows):
        for j in range(cols):
            if matrix[i][j] == 1:
                # Fill above
                if i - 1 >= 0:
                    deep_copy[i - 1][j] = 1 ^ deep_copy[i - 1][j]
                # Fill below
                if i + 1 < rows:
                    deep_copy[i + 1][j] = 1 ^ deep_copy[i + 1][j]
                # Fill left
                if j - 1 >= 0:
                    deep_copy[i][j - 1] = 1 ^ deep_copy[i][j - 1]
                # Fill right
                if j + 1 < cols:
                    deep_copy[i][j + 1] = 1 ^ deep_copy[i][j + 1]

    print(f"当前亮灯的矩阵: {deep_copy}")


def find_ones_indices(matrix):
    ones_indices = []

    for i in range(len(matrix)):
        for j in range(len(matrix[0])):
            if matrix[i][j] == 1:
                ones_indices.append((i, j))

    return ones_indices

def find_union(A, B):
    # 使用set将列表转换为集合，去除重复元素
    set_A = set(A)
    set_B = set(B)

    # 使用union方法求并集
    union_result = set_A.union(set_B)

    # 将结果转换回列表形式
    result_list = list(union_result)

    return result_list

matrix=np.zeros((3,3 ))
# matrix = np.array([[0, 0,0,0,0] ,[0, 0,0,0,0] ,[0, 0,0,0,0] ,[0, 0,0,0,0] ,[0,0,0,0,0]])
Matrix(matrix)




def inverse_matrix(A):
    n = A.shape[0]
    I = np.eye(n)
    print(n)
    print(I)

    # 构造增广矩阵 [A | I]
    augmented_matrix = np.hstack([A, I])
    print(augmented_matrix)

    # 高斯消元
    for i in range(n):
        # 将当前列的主对角元素设置为1
        factor = augmented_matrix[i, i]
        augmented_matrix[i, :] /= factor

        # 消除其他行的当前列元素
        for j in range(n):
            if i != j:
                factor = augmented_matrix[j, i]
                augmented_matrix[j, :] -= factor * augmented_matrix[i, :]

    # 提取逆矩阵部分
    inverse_A = augmented_matrix[:, n:]

    return inverse_A